package ntut.andengine.game;

import java.util.List;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.DeathLight;
import ntut.andengine.physics.DeathLight.Direction;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel10 extends GameLevel {

	GameLevel mLevel;
	List<DeathLight> deathLight1;
	List<DeathLight> deathLight2;
	List<DeathLight> deathLight3;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;

		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {

        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(360, 680));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel10.this, GameLevel6.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {}
		
		});
        
		PhysicalComponent gateF = PhysicalFactory.CreateTransmissionGate(this, new Vector2(120, 680));
		gateF.getBody().setUserData(new ContactEvent("GateF"){

			@Override
			public void beginContact(Body body, Vector2 normal) {}

			@Override
			public void endContact(Body body, Vector2 normal) {}   
		});
        
		PhysicalFactory.CreateWall(this, new Vector2(80, 20), 80, 40, ComponentColor.Blue);
		PhysicalFactory.CreateWall(this, new Vector2(20, 400), 40, 800, ComponentColor.Blue);
		PhysicalFactory.CreateWall(this, new Vector2(100, 780), 120, 40, ComponentColor.Blue);
		PhysicalFactory.CreateWall(this, new Vector2(380, 20), 120, 40, ComponentColor.Red);
		PhysicalFactory.CreateWall(this, new Vector2(460, 400), 40, 800, ComponentColor.Red);
		PhysicalFactory.CreateWall(this, new Vector2(400, 780), 80, 40, ComponentColor.Red);
		
		PhysicalFactory.CreateWall(this, new Vector2(120, 140), 160, 20, ComponentColor.Blue);
		PhysicalFactory.CreateWall(this, new Vector2(120, 340), 160, 20, ComponentColor.Blue);
		PhysicalFactory.CreateWall(this, new Vector2(120, 540), 160, 20, ComponentColor.Blue);
		PhysicalFactory.CreateWall(this, new Vector2(360, 140), 160, 20, ComponentColor.Red);
		PhysicalFactory.CreateWall(this, new Vector2(360, 340), 160, 20, ComponentColor.Red);
		PhysicalFactory.CreateWall(this, new Vector2(360, 540), 160, 20, ComponentColor.Red);
		
		PhysicalFactory.CreateDeathLight(this, new Vector2(120, 180), 200, 40, Direction.Left);
		PhysicalFactory.CreateDeathLight(this, new Vector2(120, 380), 200, 40, Direction.Left);
		PhysicalFactory.CreateDeathLight(this, new Vector2(360, 580), 200, 40, Direction.Right);
		deathLight1 = PhysicalFactory.CreateDeathLight(this, new Vector2(360, 180), 200, 40, Direction.Right);
		deathLight2 = PhysicalFactory.CreateDeathLight(this, new Vector2(360, 380), 200, 40, Direction.Right);
		deathLight3 = PhysicalFactory.CreateDeathLight(this, new Vector2(120, 580), 200, 40, Direction.Left);
		
		PhysicalFactory.CreateLaunchingPad(this, new Vector2(240, 180), true);
		PhysicalFactory.CreateLaunchingPad(this, new Vector2(240, 380), true);
		PhysicalFactory.CreateLaunchingPad(this, new Vector2(240, 580), true);
		
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(240, 40), ComponentColor.Red);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);
		mScene.registerUpdateHandler(new IUpdateHandler(){

			@Override
			public void onUpdate(float pSecondsElapsed) {

				redOrBlue();
			}

			@Override
			public void reset() {}
			
		});
		
		return mScene;
	}

	private void redOrBlue(){
		
		if(mSparkle.getPosition().x>280 && mSparkle.getPosition().y>140 && mSparkle.getPosition().y<240){
			for(DeathLight deathLight : deathLight1){
				deathLight.remove();
			}
		}
		if(mSparkle.getPosition().x>280 && mSparkle.getPosition().y>340 && mSparkle.getPosition().y<440){
			for(DeathLight deathLight : deathLight2){
				deathLight.remove();
			}
		}
		if(mSparkle.getPosition().x<200 && mSparkle.getPosition().y>540 && mSparkle.getPosition().y<640){
			for(DeathLight deathLight : deathLight3){
				deathLight.remove();
			}
		}
	}
}
